<?php
/**
 * Created by PhpStorm.
 * User: yangwang
 * Date: 2024-04-26
 * Time: 14:41
 */

namespace App\Models\Cruise;


use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;

class CruiseShopLicenseResourceRecordsModel extends BaseModel
{

    use KfUid;
    use KfSoftDeletes;

    protected $table = 'cruise_shop_license_resource_records';

    const TYPE_BUSINESS_LICENSE = 1;
    const TYPE_FOOD_LICENSE     = 2;

    const TYPE_MAP = [
        self::TYPE_BUSINESS_LICENSE => '营业执照',
        self::TYPE_FOOD_LICENSE     => '食品许可证',
    ];

    const LICENSE_FOREVER_DATE = 20991231;

    public static function licenseIsForever($licenseEndDateTimeStamp): bool
    {
        return $licenseEndDateTimeStamp == strtotime(self::LICENSE_FOREVER_DATE)
            || $licenseEndDateTimeStamp == strtotime('29991231');
    }

    public function recordRel()
    {
        $this->hasOne(CruiseShopLicenseRecordsModel::class, 'shopId', 'shopId');
    }

    public static function getDataMappingFunctionOpen($shopId): array
    {
        $resources = self::query()
                         ->where('shopId', $shopId)
                         ->where('status', CruiseShopLicenseRecordsModel::STATUS_APPROVED)
                         ->get()
                         ->keyBy('type')
                         ->toArray();

        //食品经营许可证和营业执照都通过的才回显
        if (empty($resources) || count($resources) != 2) {
            return [];
        }

        $fieldMap = [
            self::TYPE_BUSINESS_LICENSE => [
                'businessLicenseImg'    => 'url',
                'enterpriseName'        => 'contents.licenseName',//企业名称
                'businessLicenseNumber' => 'licenceNumber',//营业执照信用代码
                'businessUserName'      => 'legalPerson',//
            ],
            self::TYPE_FOOD_LICENSE     => [
                'foodBusinessLicenseImg' => 'url',
                'legalPerson'            => 'legalPerson',//法定代表人/负责人
                'permitLicenseNumber'    => 'licenceNumber',//许可证编号
                'businessPremises'       => 'contents.productionAddress',//经营地址
                'effectiveTimeStartDate' => 'licenseStartDate',
                'effectiveTimeEndDate'   => 'licenseEndDate'
            ]
        ];

        $result = [];
        foreach ($fieldMap as $type => $filedList) {
            foreach ($filedList as $field => $localField) {
                $contents = json_decode($resources[$type]['contents'] ?: '[]', 1);
                if (str_contains($localField, 'contents')) {
                    $value = attributeGet($contents, $localField, '');
                } else {
                    $value = attributeGet($resources[$type], $localField, '');
                }

                if ($localField == 'url') {
                    $value = [picturePath($value)];
                }

                if (in_array($localField, ['licenseStartDate', 'licenseEndDate'])) {
                    $value = $value ? date('Y-m-d', $value) : '';
                }

                $result[$field] = $value;
            }
        }

        return $result;
    }

}